<?php
$cs = Yii::app()->getClientScript();
$cs->registerScriptFile(Yii::app()->request->baseUrl.'/js/jquery.raty.js');
?>
<img style="max-width:100%" src="upload/<?php echo $model->picture; ?>" /><br/>
<div class="row">
Rating: <div id="star" data-score="<?php echo $model->rating; ?>"></div>
</div>
<style>
a#fav{
 background: url('./images/favorite_add.png');
 display: block;
 width: 32px;
 height: 32px;
}

a#fav.active{
 background: url('./images/favorites_delete3.png');
}
</style>
<div class="row">
    <a href="#" id="fav" title="[+] Add as favorite">&nbsp;</a>
</div>
<?php
$this->widget('ext.mobile.ListView', array(
    'dataProvider'=>$sportTypes,
    'dividerName'=>'',
    'itemView'=>'_viewSportType',
    'dataFilter'=>false,
    'otherInfo'=>array('id'=>$model->id,),
));
?>
<script>
$('#mainPage').on('pageshow', function () {
    $('#star').raty({
        path: "<?php echo Yii::app()->request->baseUrl; ?>/images/",
        readOnly: true,
        score: function() {
            return $(this).attr('data-score');
        }
    });
    
<?php if ($favorite == null) { ?>
    $('a#fav').bind('click', addFav);
<?php } else { ?>
    changeToRemoveFav();
<?php } ?>
});

function changeToRemoveFav() {
    $('a#fav')
        .addClass('active')
        .attr('title','[-] Remove from favorites')
        .unbind('click')
        .bind('click', removeFav)
    ;
}

function addFav() {
    $.ajax({
      url: "<?php echo $this->createUrl('addFavorite', array("id"=>$model->id)); ?>",
      data: {},
      success: function(){
           changeToRemoveFav();
      }
    });
}

function removeFav(){
    $.ajax({
      url: "<?php echo $this->createUrl('removeFavorite', array("id"=>$model->id)); ?>",
      data: {},
      success: function(){
            $('a#fav')
                 .removeClass('active')
                 .attr('title','[+] Add as favorite')
                 .unbind('click')
                 .bind('click', addFav)
            ;
      }
    });
}
</script>
